package com.gulimall.product.service.impl;

import com.gulimall.common.dto.cart.CartItemDTO;
import com.gulimall.product.domain.entity.SkuImages;
import com.gulimall.product.domain.entity.SkuInfo;
import com.gulimall.product.domain.entity.SpuInfoDesc;
import com.gulimall.product.domain.vo.SkuItemVo;
import com.gulimall.product.mapper.SkuInfoMapper;
import com.gulimall.product.service.ISkuImagesService;
import com.gulimall.product.service.ISkuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.product.service.ISpuInfoDescService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 * sku信息 服务实现类
 * </p>
 *
 * @author 坤哥
 * @since 2025-08-25
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements ISkuInfoService {
    @Resource
    private ISkuImagesService skuImagesService;
    @Resource
    private ISpuInfoDescService spuInfoDescService;
    @Resource
    private SkuInfoMapper skuInfoMapper;
    @Resource
    private ThreadPoolExecutor executor;

    /**
     * 获取商品详情信息
     * 异步编排
     */
    @Override
    public SkuItemVo getItemInfo(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        //sku基本信息获取,pms_sku_info
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        }, executor);

        //  sku的图片信息，pms_sku_images
        CompletableFuture<Void> skuImgFuture = CompletableFuture.runAsync(() -> {  //无需返回值
            List<SkuImages> skuImagesList = skuImagesService.lambdaQuery().eq(SkuImages::getSkuId, skuId).list();
            skuItemVo.setSkuImages(skuImagesList);
        }, executor);

        //获取spu的销售属性组合
        CompletableFuture<Void> skuSaleFuture = skuInfoFuture.thenAcceptAsync((result) -> {
            List<SkuItemVo.SkuItemSaleAttrVo> skuItemSaleAttrVos = skuInfoMapper.getSkuSaleAttrs(result.getSpuId());
            skuItemVo.setSaleAttrs(skuItemSaleAttrVos);
        }, executor);

        //获取spu的介绍
        CompletableFuture<Void> spuInfoDescFuture = skuInfoFuture.thenAcceptAsync((result) -> {
            SpuInfoDesc spuInfoDesc = spuInfoDescService.lambdaQuery().eq(SpuInfoDesc::getSpuId, result.getSpuId()).one();
            skuItemVo.setSpuInfoDesc(spuInfoDesc);
        }, executor);

        //获取spu的规格参数信息
        CompletableFuture<Void> spuItemBaseAttrFuture = skuInfoFuture.thenAcceptAsync((result) -> {
            List<SkuItemVo.SpuItemBaseAttrVo> spuItemBaseAttrVos = skuInfoMapper.getSpuAttrInfo(result.getSpuId());
            skuItemVo.setSpuItemBaseAttrVos(spuItemBaseAttrVos);
        }, executor);

        CompletableFuture.allOf(skuInfoFuture,skuImgFuture,skuSaleFuture,spuInfoDescFuture,spuItemBaseAttrFuture).get();
        return skuItemVo;
    }

    /**
     * 获取商品sku信息
     * psm_sku_info
     */
    @Override
    public CartItemDTO getSkuInfo(Long skuId) {
        SkuInfo skuInfo = lambdaQuery().eq(SkuInfo::getSkuId, skuId).one();
        CartItemDTO cartItemDTO = new CartItemDTO();
        cartItemDTO.setSkuId(skuInfo.getSkuId());
        cartItemDTO.setTitle(skuInfo.getSkuTitle());
        cartItemDTO.setImage(skuInfo.getSkuDefaultImg());
        cartItemDTO.setPrice(skuInfo.getPrice());
        cartItemDTO.setCount(2);
        cartItemDTO.setTotalPrice(skuInfo.getPrice().multiply(BigDecimal.valueOf(2)));
        return cartItemDTO;
    }

    //    @Override
//    public List<SkuItemVo.SpuItemBaseAttrVo> getSpuInfo() {
//        List<SkuItemVo.SpuItemBaseAttrVo> spuItemBaseAttrVos = skuInfoMapper.getSpuAttrInfo();
//        return spuItemBaseAttrVos;
//    }
}
